package com.yazi.mxz.person.controller;
import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.jackson.result.ObjectResult;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.entity.enums.RoleType;
import com.yazi.mxz.common.service.StoreService;


@Controller("commonController")
@RequestMapping("/common")
public class CustomCommonController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(CustomCommonController.class);
	
	@Resource
	StoreService storeService;
	
	@RequestMapping("/orderTip/json")
	@ResponseBody
	public ObjectResult<?> orderCountJson() {
		ObjectResult<Boolean> jsonResult = new ObjectResult<Boolean>();
		Subject subject;
		try {
			subject = SecurityUtils.getSubject();
			// 已经登录并具有店铺角色
			if (subject.isAuthenticated() && subject.hasRole(RoleType.SHOP.name())) {
				Store store = getStore(true);
				
				jsonResult.setResult(store.getIsNewOrder());
				jsonResult.setOkMessage("店铺有新订单");
			} else {
				jsonResult.setFail();
			}
		} catch (Exception e) {
			log.error("获取订单通知异常:", e);
			jsonResult.setError();
		}
		
		return jsonResult;
	}
	
	@RequestMapping("/clearOrderTip/json")
	@ResponseBody
	public StatusResult clearOrderTip() {
		StatusResult jsonResult = new StatusResult();
		Subject subject = SecurityUtils.getSubject();
		try {
			// 已经登录并具有店铺角色
			if (subject.isAuthenticated() && subject.hasRole(RoleType.SHOP.name())) {
				Store store = getStore(true);
				store.setIsNewOrder(false);
				storeService.merge(store);
				jsonResult.setOk();
			} else {
				jsonResult.setFail();
			}
		} catch (Exception e) {
			log.error("清空订单通知异常:", e);
			jsonResult.setError();
		}
		
		return jsonResult;
	}
	
}
